package com.gsq.learning.mybatisplugin.plugin.interceptor;

import org.apache.ibatis.binding.MapperMethod;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.plugin.*;

import java.sql.PreparedStatement;
import java.util.Properties;

@Intercepts({
        @Signature(
                type = ParameterHandler.class,
                method = "setParameters",
                args = {PreparedStatement.class}
        )
})
public class MyParameterInterceptor implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {

        ParameterHandler target = (ParameterHandler) invocation.getTarget();
        System.out.println("======> MyParameterInterceptor ：" + target);

        MapperMethod.ParamMap parameterObject = (MapperMethod.ParamMap) target.getParameterObject();
        if (parameterObject != null) {
            //Class<?> aClass = parameterObject.getClass(); // class org.apache.ibatis.binding.MapperMethod$ParamMap
            System.out.println("======> parameterObject = " + parameterObject);
            parameterObject.put("age", 16); // 这里修改了 age 的值
            System.out.println("======> parameterObject = " + parameterObject);
        }

        Object proceed = invocation.proceed();
        return proceed;
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {

    }
}
